package com.nami.store.biz.conver.service.impl;

import com.nami.store.api.request.conversation.ConverC2CUpdRequest;
import com.nami.store.biz.conver.repository.ConverRepository;
import com.nami.store.biz.conver.service.PrivateConverDomainService;
import com.nami.store.domain.ConversationDO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.enums.conver.ConverTypeIntEnum;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class PrivateConverDomainServiceImpl implements PrivateConverDomainService {

    private final ConverRepository converRepository;

    @Override
    public boolean updPrivateChatConver(ConverC2CUpdRequest request) {

        ConversationDO conversation = new ConversationDO();
        conversation.setAppKey(request.getAppKey());
        conversation.setConverType(ConverTypeIntEnum.C2C.getCode());

        conversation.setUserId(request.getTargetId());
        conversation.setTargetId(request.getUserId());

        conversation.setLatestUnreadMsgIndex(request.getLatestUnReadMsgIndex());
        conversation.setLatestMsg(request.getLatestMsg());
        conversation.setLatestMsgId(request.getLatestMsgId());
        conversation.setSyncTime(request.getSyncTime());
        conversation.setSortTime(request.getSortTime());

        converRepository.insertOrUpd(conversation);
//        converRepository.updateReceiverConver(conversation);

        ConversationDO myself = new ConversationDO();
        myself.setAppKey(request.getAppKey());
        myself.setConverType(ConverTypeIntEnum.C2C.getCode());
        myself.setUserId(request.getUserId());
        myself.setTargetId(request.getTargetId());

        myself.setLatestMsg(request.getLatestMsg());
        myself.setLatestMsgId(request.getLatestMsgId());

        myself.setSyncTime(request.getSyncTime());
        myself.setSortTime(request.getSortTime());
        converRepository.updateMySelfConver(myself);

        return Boolean.TRUE;
    }
}
